package com.penton.util.widget.treelistview;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

/**
 * ����ResManager.java
 * @author wader
 * ����������ȡ������assetsĿ�µ����֡�ͼƬ����Դ
 * ����ʱ�䣺2011-11-29 16:07
 */
public class ResManager {
    /**
     * ͼƬ��ʽ
     */
    private static final String IMAGE_FILE_FORMAT = ".png";
    /**
     * �ı��ļ���ʽ
     */
    private static final String TEXT_FILE_FORMAT = ".properties";
 
    /**
     * ͼƬ��ŵ�·��
     */
    public final static String IMAGES_DIR = "images/";
 
    // public final static String IMAGES_DIR_480 = "images_480/";
    public final static String TEXTS_DIR = "textRes/";
    /**
     * �ļ�·��
     */
    private static String filePath = "";
 
    /**
     * �ӹ�����Դ����ͼƬ��Դ��·����assets/images/**.png��
     *
     * @param fileName
     *            ͼƬ��Դ·��
     */
    public static Bitmap loadImageRes(Activity activity, int screenWidth,
           String fileName) {
       Bitmap bitmap = null;
       InputStream is = null;
       FileInputStream fis = null;
       filePath = IMAGES_DIR;
       // ������Ը�ݷֱ��ʵȽ���·������ж�
       // if (screenWidth >= 480) {
       // filePath = IMAGES_DIR_480;
       // }
       try {
           is = activity.getAssets().open(
                  filePath + fileName + IMAGE_FILE_FORMAT);
           if (is != null) {
              bitmap = BitmapFactory.decodeStream(is);
           }
       } catch (Exception e) {
       } finally {
           try {
              if (is != null) {
                  is.close();
              }
              if (fis != null) {
                  fis.close();
              }
           } catch (Exception e) {
           } finally {
              is = null;
              fis = null;
           }
       }
       return bitmap;
    }
 
    /**
     * �ӹ�����Դ����������Դ��·���ǣ�assets/textRes/**.properties��
     *
     * @param fileName
     */
    public static ArrayList<String> loadTextRes(String fileName, Context context) {
       filePath = TEXTS_DIR;
       return loadProperties(filePath + fileName + TEXT_FILE_FORMAT, context);
    }
 
    /**
     * ��ȡ�����ļ���ȡ������Ϣ
     *
     * @param filename
     *            �����ļ�·��
     * @return ��������Ϣ��hashmap��ֵ��
     */
    private static ArrayList<String> loadProperties(String filename,
           Context context) {
       Log.d("loadProperties", "loadProperties");
       ArrayList<String> properties = new ArrayList<String>();
       InputStream is = null;
       FileInputStream fis = null;
       InputStreamReader rin = null;
 
       // �������ļ��ŵ�res/raw/Ŀ¼�£�����ͨ�����µķ�����ȡ
       // is = context.getResources().openRawResource(R.raw.system);
 
       // ���Ƕ�ȡ�����ļ��ĵڶ��ַ���
       // �������ļ��ŵ�assetsĿ¼�£�����ͨ�����µķ�����ȡ
       // is = context.getAssets().open("system.properties");
 
       // ������ȡ��ֵ�Ե���ʱ�ַ�
       StringBuffer tempStr = new StringBuffer();
 
       // ������Ŷ�ȡ��ÿ���ַ�
       int ch = 0;
 
       // ���������ȡ�������ļ�һ�е���Ϣ
       String line = null;
       try {
           Log.d("loadProperties", "the filename is: " + filename);
           is = context.getAssets().open(filename);
           rin = new InputStreamReader(is, "UTF-8");
 
           while (ch != -1) {
              tempStr.delete(0, tempStr.length());
              while ((ch = rin.read()) != -1) {
                  if (ch != '\n') {
                     tempStr.append((char) ch);
                  } else {
                     break;
                  }
              }
              line = tempStr.toString().trim();
              Log.d("loadProperties", "line: " + line);
              // �ж϶�������������Ƿ���Ч,#��ͷ�Ĵ��ע��,�����ע������ô�������,�����������
              if (line.length() == 0 || line.startsWith("#")) {
                  continue;
              }
              properties.add(line);
           }
       } catch (IOException e) {
           // LogX.trace("read property file", e.getMessage() + "fail");
       } finally {
           try {
              if (is != null) {
                  is.close();
              }
              if (fis != null) {
                  fis.close();
              }
              if (null != rin) {
                  rin.close();
              }
           } catch (IOException e) {
              // LogX.trace("read property file", e.getMessage() + "fail");
           } finally {
              is = null;
              fis = null;
              rin = null;
           }
       }
       return properties;
    }
}
